#!/usr/bin/env bash

# 定义项目路径和目标路径
CAS_SERVER_DIR="./redragon-erp/CasServer"
ERP_PARENT_DIR="./redragon-erp/erp-parent"
WAR_TARGET_DIR="./tmp/wars"

# 创建目标目录（如果不存在）
if [ ! -d "$WAR_TARGET_DIR" ]; then
   echo "War files not found!"
   mkdir -p $WAR_TARGET_DIR
fi

# 使用 Maven 编译项目
echo "Compiling $CAS_SERVER_DIR"
mvn clean install -f "$CAS_SERVER_DIR/pom.xml"
if [ $? -eq 0 ]; then
  echo "Done. All war files have been copied to $WAR_TARGET_DIR"
else
  echo "Error: Compile $CAS_SERVER_DIR failed! Check your compile settings."
  exit 1
fi

echo "Compiling $ERP_PARENT_DIR"
mvn clean install -f "$ERP_PARENT_DIR/pom.xml"
if [ $? -eq 0 ]; then
  echo "Done. All war files have been copied to $WAR_TARGET_DIR"
else
  echo "Error: Compile $ERP_PARENT_DIR failed! Check your compile settings."
  exit 1
fi

# 查找所有生成的 .war 文件并复制到目标目录
echo "Searching for .war files in ./redragon-erp/ and copying to $WAR_TARGET_DIR"
rm -rf ./tomcat/webapps/*
find ./redragon-erp/ -name "*.war" -exec cp {} "$WAR_TARGET_DIR" \;
if [ $? -eq 0 ]; then
  echo "Done. All war files have been copied to $WAR_TARGET_DIR"
else
  echo "Error: Cannot be installed! Check your permission."
  exit 1
fi

